package com.ruoyi.product.controller;


import com.ruoyi.common.core.exception.BaseException;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.product.domain.Goods;
import com.ruoyi.product.service.IGoodsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class GoodsActionParamHandler extends AbstractActionParamHandler<Long, Goods> {
    @Autowired
    private IGoodsService goodsService;

    @Override
    public Goods list(Goods bean) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            bean.setMid(getMerchantId());

        }
        setSort();
        return bean;
    }

    @Override
    public Goods export(Goods bean) {
        
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {

            bean.setMid(getMerchantId());
        }
        setSort();
        return bean;
    }

    @Override
    public Long getInfo(Long id) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {

            Long merchantId = getMerchantId();
            if (ObjectUtils.notEqual(merchantId,goodsService.selectGoodsById(id).getMid())) {
                throw new BaseException("非本公司数据,不能查询");
            }
        }
        return id;
    }

    @Override
    public Goods add(Goods bean) {

        String username = SecurityUtils.getUsername();

        bean.setMid(getMerchantId());
        Date now = new Date();
        bean.setCreateTime(now);

        bean.setCreateBy(username);
        bean.setUpdateTime(now);
        bean.setUpdateBy(username);
        return bean;
    }

    @Override
    public Goods edit(Goods bean) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {

            Long merchantId = getMerchantId();
            if (ObjectUtils.notEqual(merchantId,goodsService.selectGoodsById(bean.getId()).getMid())) {
                throw new BaseException("非本公司数据,不能修改");
            }
            bean.setMid(getMerchantId());

        }
        String username = SecurityUtils.getUsername();
        Date now = new Date();

        bean.setUpdateTime(now);
        bean.setUpdateBy(username);
        return bean;
    }

    @Override
    public Long[] remove(Long[] ids) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            Long merchantId = getMerchantId();

            List<Goods> list =goodsService.selectGoodsByIds(ids);
            for (Goods bean : list) {
                if (ObjectUtils.notEqual(merchantId,goodsService.selectGoodsById(bean.getId()).getMid())) {
                    throw new BaseException("非本公司数据,不能修改");
                }
            }
        }
        return ids;
    }

    @Override
    public Goods treeselect(Goods bean) {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            bean.setMid(getMerchantId());
        }
        return bean;
    }
}
